#include "block.h"
#include "ui_block.h"
#include "finishatt.h"
#include "des.h"
#include <QFileDialog>
#include <QMessageBox>
#include <fstream>
#include <QProcess>

Block::Block(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Block)
{
    ui->setupUi(this);
}

Block::~Block()
{
    delete ui;
}

void Block::on_sourcebutton_clicked()
{
    QString source = QFileDialog::getOpenFileName(this,"open",QCoreApplication::applicationFilePath(),"");
    if(!source.isEmpty()){
        ui->sourcetxtpath->setText(source);
    }

}


void Block::on_objectivebutton_clicked()
{
    QString objective = QFileDialog::getOpenFileName(this,"open",QCoreApplication::applicationFilePath(),"");
    if(!objective.isEmpty()){
        ui->objectivetxtpath->setText(objective);
    }

}


void Block::on_pushButton_clicked()
{
    QString sou = ui->sourcetxtpath->toPlainText();//需加密文件地址
    QString obj = ui->objectivetxtpath->toPlainText();//加密后新文件存放的地址
    QString k = ui->keyedit->displayText();

    if(sou.isEmpty())
        QMessageBox::warning(this,"Attention","please choose the source text");
    else if(obj.isEmpty())
        QMessageBox::warning(this,"Attention","please choose the objective text");
    else if(k.isEmpty())
        QMessageBox::warning(this,"Attention","please input the key");
    else{

        Des des;
        std::ifstream in;
        std::ofstream out;
        std::bitset<64> ptxt,ctxt;

        des.key = des.charToBitset(k.toStdString().c_str());

        des.generalkey();

        in.open(sou.toStdString().c_str(),std::ios::binary);
        out.open(obj.toStdString().c_str(),std::ios_base::out|std::ios_base::binary);
        while(in.read((char*)&ptxt,sizeof(ptxt))){
                        ctxt=des.encryt(ptxt);
                        out.write((char*)&ctxt,sizeof(ctxt));
                        ptxt.reset();
        }
        in.close();
        out.close();
        Finishatt *fi = new Finishatt;
        fi->show();
    }
}


void Block::on_pushButton_2_clicked()
{
    QString sou = ui->sourcetxtpath->toPlainText();
    QString obj = ui->objectivetxtpath->toPlainText();
    QString k = ui->keyedit->displayText();

    if(sou.isEmpty())
        QMessageBox::warning(this,"Attention","please choose the source text");
    else if(obj.isEmpty())
        QMessageBox::warning(this,"Attention","please choose the objective text");
    else if(k.isEmpty())
        QMessageBox::warning(this,"Attention","please input the key");
    else{
        Des des;
        std::ifstream in;
        std::ofstream out;
        std::bitset<64> ptxt,ctxt;

        des.key = des.charToBitset(k.toStdString().c_str());

        des.generalkey();

        in.open(sou.toStdString().c_str(),std::ios::binary);
        out.open(obj.toStdString().c_str(),std::ios_base::out|std::ios_base::binary);
        while(in.read((char*)&ptxt,sizeof(ptxt))){
                        ctxt=des.decryt(ptxt);
                        out.write((char*)&ctxt,sizeof(ctxt));
                        ptxt.reset();
                    }
        in.close();
        out.close();
        Finishatt *fi = new Finishatt;
        fi->show();
    }

}

